// wode/yuliang/yuliang.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    isShowConfirm:false 
  },

  //点击领取 更改用户value 并改变getvalue信息
  Togetvalue:function(e){
    let num=e.currentTarget.dataset.num
    let id=e.currentTarget.dataset.id
    let can=e.currentTarget.dataset.can
    let did=e.currentTarget.dataset.did

    //如果点击次数等于可点击次数则不可再进行领取
    if(did<can){

      //更改getvalue信息
      wx.request({
        url: `https://www.prebeauty.work:3001/api/value/${id}`,
        data:{
          getvaluedid:did+1
        },
        header: {
          'content-type': 'application/json'
        },
        method: 'PUT',
        success:(res)=>{
          console.log("get value click success!")

          //更改本地getvalue信息
          let values=this.data.valuelist
          for(let i=0;i<values.length;i++){
            if(values[i].getvalueid==id){
              ++values[i].getvaluedid
            }
          }
          this.setData({
            valuelist:values
          })
        }
      })
      
      
      //更改用户value值
      wx.request({
        url: `https://www.prebeauty.work:3001/api/userpage/${this.data.id}`,
        data:{
          uservalue:this.data.user.uservalue+num
        },
        header: {
          'content-type': 'application/json'
        },
        method: 'PUT',
        success:(res)=>{
          console.log("getvalue success!")
          
          //更改本地用户value值
          let userr=this.data.user
          userr.uservalue=this.data.user.uservalue+num
          this.setData({
            user:userr
          })
        }
      })
      

    }
    
  },

  //兑换输入姓名\手机号\地址
  setName: function (e) {
    this.setData({
      inputName: e.detail.value
    })
  },
  setPhone: function (e) {
    this.setData({
      inputPhone: e.detail.value
    })
  },
  setAddr: function (e) {
    this.setData({
      inputAddr: e.detail.value
    })
  },
  //点击取消
  cancel: function () {
    this.setData({
      isShowConfirm: false,
    })
  },
  //点击确认->兑换
  confirmAcceptance:function(){
    //添加兑换订单
    wx.request({
      url: 'https://www.prebeauty.work:3001/api/exchange',
      data:{
        userid:this.data.id,
        exchangename:this.data.goodsname,
        username:this.data.inputName,
        userphone:this.data.inputPhone,
        useraddr:this.data.inputAddr,
        goodsid:this.data.goodsid,
        exchangey:"true"
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success:(res)=>{
        console.log("exchange success!")
      }
    })
    //改变用户value
    wx.request({
      url: `https://www.prebeauty.work:3001/api/userpage/${this.data.id}`,
      data:{
        uservalue:this.data.user.uservalue-this.data.goodsvalue
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'PUT',
      success:(res)=>{
        console.log("getvalue success!")
      }
    })

    //改变本地value
    let userr=this.data.user
    userr.uservalue=this.data.user.uservalue-this.data.goodsvalue
    this.setData({
      user:userr
    })

    //关闭输入弹窗
    this.setData({
      isShowConfirm: false
    })

    //兑换成功
    wx.showToast({
      title: '兑换成功！',
      icon:'success',
      duration: 2000
    })
    
  },

  //点击兑换
  exchange:function(e){
    //获取用户当前value值
    let uservalue=this.data.user.uservalue

    //获取所要兑换商品的value\name\goodsid
    let value=e.currentTarget.dataset.value
    let name=e.currentTarget.dataset.name
    let id=e.currentTarget.dataset.id
    this.setData({
      goodsname:name,
      goodsvalue:value,
      goodsid:id
    })
    //用户value不足以兑换此商品
    if(uservalue<value){
      wx.showToast({
        title: '狗粮不足,兑换失败！',
        icon: 'none',
        duration: 2000
      })
    }else{
      //可以兑换 弹出输入弹窗
      this.setData({
        isShowConfirm: true
      })
    }
  },

  //生命周期函数--监听页面加载
  onLoad: function (options) {
    console.log(this.data.id)
    this.setData({
      id: wx.getStorageSync('userid'),
      img:wx.getStorageSync('userimg'),
      name:wx.getStorageSync('username'),
    })

    //获取用户信息 显示value值
    wx.request({
      url: `https://www.prebeauty.work:3001/api/userpage?userid=${this.data.id}`,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      success:(res)=>{
        console.log(res.data[0])
        this.setData({
          user:res.data[0]
        })
      }
    })

    //获取可兑换狗粮列表
    wx.request({
      url: 'https://www.prebeauty.work:3001/api/value',
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      success:(res)=>{
        console.log(res.data)
        let values=res.data
        let valuelist=[]
        for(let i=0;i<values.length;i++){
          if(values[i].getvalueuser==this.data.id){
            valuelist.push(values[i])
          }
        }
        this.setData({
          valuelist:valuelist
        })
      }
    })

    //获取可兑换商品列表
    wx.request({
      url: `https://www.prebeauty.work:3001/api/goods`,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      success:(res)=>{
        console.log(res.data)
        let goodslist=res.data
        let goods=[]
        for(let i=0;i<goodslist.length;i++){
          if(goodslist[i].exchange=='true'){
            goods.push(goodslist[i])
          }
        }
        this.setData({
          goodslist:goods
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(!wx.getStorageSync('token')){
      wx.showModal({
        title: '用户未登录',
        content: '请先进行授权登录',
        success(res) {
          if (res.confirm) {
            console.log('确定')
            wx.switchTab({
              url: '/pages/home/home'
           })
          } else if (res.cancel) {
            console.log('取消')
            wx.switchTab({
              url: '/pages/index/index'
           })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})